<!-- 作者:唐嘉诚 -->
<!-- 修改院线 -->
<!-- diijaiwji -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title></title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
    <style>
        .imgs{
            margin-left: 110px;
            margin-top: 10px;
            position: relative;
            width:150px;
            height:150px;
            /* display: none; */
        }
        .imgs i{
            position: absolute;
            right: 2px;
            top: 2px;
            color:#fff;
            z-index:1;
            cursor: pointer;
        }
        .imgs::after{
            content: "";
            position:absolute;
            height:20px;
            background:rgba(0,0,0,.5);
            top:0;
            left:0;
            width:100%;
            border-radius: 5px 5px 0 0;
        }
        .imgs img{
            width:100%;
            height:100%;
            display: block;
            object-fit: contain;
            border-radius:5px;
            border: 1px solid gray;
            box-sizing: border-box;
        }
    </style>
</head>
<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-body" style="position:absolute;left:50px;top:40px;">
            <button class="layui-btn layui-btn-sm" onclick="location.href = 'index.html'">返回</button>
            <div style="padding: 15px;">
                <form class="layui-form" lay-filter="updateForm" action="" onsubmit="return false">
                    <div class="layui-form-item">
                        <label class="layui-form-label">院线名称</label>
                        <div class="layui-input-inline">
                            <input type="text" style="width:400px" name="name" lay-verify="required" placeholder="请输入院线名称"
                                autocomplete="off" class="layui-input" id="name_cinema">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">院线地址</label>
                        <div class="layui-input-inline">
                            <input type="text" style="width:600px" name="addr" lay-verify="required" placeholder="请输入院线地址"
                                autocomplete="off" class="layui-input" id="addr_cinema">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">图片</label>
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn" id="test1">
                                <i class="layui-icon">&#xe67c;</i>上传图片
                            </button>
                            <div class="imgs" style="margin-left:0px">
                                <img src="" alt="" id="img_cinema">
                                <i class="layui-icon layui-icon-close"></i>
                            </div>
                            <div class="img-input" style="display:none">
                                <input type="text" id="txtimg" name="cover">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">联系电话</label>
                        <div class="layui-input-inline">
                            <input type="text" name="tel" lay-verify="required" placeholder="请输入联系电话"
                                autocomplete="off" class="layui-input" id="phont_cinema">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit="" lay-filter="cinema-update">修改院线</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>
    <script>
        //得到一个url地址中的所有参数
        function query(url) {
            const parts = location.href.split("?");
            if (parts.length == 1) {
                return {};
            }
            const obj = {};
            parts[1].split("&").map(item => item.split("=")).forEach(([p, v]) => {
                obj[p] = decodeURIComponent(v);
            });
            return obj;
        }
        const q=query();
        layui.use(['form'], async function () {
            const form = layui.form;
            const cinema=await $.get("/api/Cinema/"+q.id);
            $(".imgs img").prop("src",cinema.data.image);
            form.val("updateForm",cinema.data);
            form.on('submit(cinema-update)', async function (data) {
                await $.ajax({
                    url: "/api/Cinema/" + q.id,
                    method: "put",
                    data:{
                        name:$("#name_cinema").val(),
                        addr:$("#addr_cinema").val(),
                        image:$("#img_cinema").prop("src"),
                        tel:$("#phont_cinema").val()
                    },
                    success:function(){
                        location.href = "./index.html"
                    }
                });
            });
        });
        layui.use('upload', function () {
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#test1', //绑定元素
                url: '/api/upload', //上传接口
                field: "imgfile",
                acceptMime: "image/*",
                size: 500,
                multiple: true,
                drag: true,
                done: function (res) {
                    //上传完毕回调
                    $(".imgs img").prop("src", res.data);
                    $("#txtimg").val(res.data[0]);
                    $(".imgs").show();
                }
                , error: function () {
                    //请求异常回调
                    console.log("错误");
                }
            });
        });

        $(".imgs i").click(function () {
            $(".imgs img").prop("src", "");
            $("#txtimg").val("");
            $(".imgs").hide();
        })
    </script>
</body>
</html>